package cn.springcloud.fix.demo.thread;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * https://www.cnblogs.com/hanganglin/p/3526240.html
 */
public class TimerTest {

    //被执行的任务必须继承TimerTask，并且实现run方法
    static class MyTimerTask1 extends TimerTask {
        public void run() {
            System.out.println("爆炸！！！");
        }
    }

    public static void main(String[] args) throws ParseException {
        Timer timer = new Timer();
        //1、设定两秒后执行任务，然后没隔一秒再执行一次
        timer.scheduleAtFixedRate(new MyTimerTask1(), 2000, 1000);
        //2、设定任务在执行时间执行，本例设定时间13:57:00
        // SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        // Date time = dateFormatter.parse("2020/06/19 23:35:00");
        // timer.schedule(new MyTimerTask1(), time);
    }

    private final static ScheduledThreadPoolExecutor schedual = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {

        private AtomicInteger atoInteger = new AtomicInteger(0);

        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("xxx-Thread " + atoInteger.getAndIncrement());
            return t;
        }
    });
}
